package main;

/*
 * To change this template, choose Tools | Templates and open the template in
 * the editor.
 */
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

/**
 *
 * @author daz
 */
public class Card {

    public static final int STATE_LOW = 0;
    public static final int STATE_NORMAL = 1;
    public static final int STATE_HEIGHT = 2;
    public static final int DOWN = 0;
    public static final int UP = 1;
    public static final int STATE_MOVE = 3;
    public static final int STATE_HIDE = 4;
    private Image cardImage;
    protected boolean isCardEated;
    protected int phomId;
    protected int eatId;
    private int state;
    private int cardX;
    private int cardY;
    private int cardId;
    private int cardWidth;
    private int cardHeight;
    private int step;
    private int deltaMoveX;
    private int deltaMoveY;
    private int countStep;
    private int cardIdHide;

    public Card(int id, int state, Image image) {
        if (state == STATE_HIDE) {
            cardIdHide = id;
            cardId = 52;
        } else {
            this.cardId = id;
            cardIdHide = -1;
        }
        this.cardImage = image;
        this.state = state;
        this.cardHeight = image.getHeight();
        this.cardWidth = image.getWidth() / 53;
    }

//    public void setCardId(int id) {
//        cardId = id;
//    }
    public int getCardId() {
        return this.cardId;
    }

    public void setX(int x) {
        this.cardX = x;
    }

    public int getX() {
        return this.cardX;
    }

    public int getY() {
        return this.cardY;
    }

    public int getWidth() {
        return this.cardWidth;
    }

    public int getHeight() {
        return this.cardHeight;
    }

    public int getState() {
        return this.state;
    }

    public void setState(int state) {
        this.state = state;
    }

    public void setImage(Image image) {
        this.cardImage = image;
        this.cardHeight = image.getHeight();
        this.cardWidth = image.getWidth();

    }

    public void setPosition(int x, int y) {
        cardX = x;
        cardY = y;
//        Debug.d("card set x:" + x);
//        Debug.d("card set x:" + y);
    }

    public void changeState(int type) {
        if (type == DOWN) {
            state--;
            if (state < 0) {
                state = 0;
            } else {
                cardY += 10;
            }

        } else if (type == UP) {
            state++;
            if (state > STATE_HEIGHT) {
                state = STATE_HEIGHT;
            } else {
                cardY -= 10;
            }


        }
    }

    public void initializeMove(int srcX, int srcY, int desX, int desY, int step) {
        deltaMoveX = desX - srcX;
        deltaMoveY = desY - srcY;
        this.step = step;
        if (step > 3) {
            this.step = 3;
        }
        cardX = srcX;
        cardY = srcY;
        countStep = 0;
        state = STATE_MOVE;
    }

    public void move() {
        //  Debug.d("x:" + cardX);
        // Debug.d("y:" + cardY);
        if (countStep == step) {
            state = STATE_NORMAL;

        } else {
            cardX += deltaMoveX / step;
            cardY += deltaMoveY / step;

        }
        countStep++;
    }

    public void openCard() {
        if (cardIdHide != -1) {
            cardId = cardIdHide;
        }
    }

    public void setCardId(int id) {
        this.cardId = id;
    }

    public void paint(Graphics g) {
        Utils.drawFrame(g, cardImage, cardX, cardY, cardId, 0, cardWidth, cardHeight);
    }
//    public void paint(Graphics g, int x, int y) {
//        cardX = x;
//        cardY = y;
//        paint(g);
//    }
}
